<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/30
 * Time: 21:53
 */

namespace Xcx\Model;

class GoodsBargainModel extends BaseModel
{

    /**
     * 添加订单砍价信息
     * @param $orderNo
     * @return mixed
     */
    public function addBargainInfo($orderNo)
    {
        $OrderChild = new GoodsOrderChildModel();
        $goodsList = $OrderChild->getOrderGoodsList($orderNo);
        $totalFee = 0;
        foreach ($goodsList as $v) {
            $totalFee += $v['price'] * $v['count'];
        }
        $data['limit_cut'] = floor($totalFee*0.5);
        $data['order_no'] =$orderNo;
        $data['uid'] = $this->uid;
        $data['add_time'] = time();

        return $this->data($data)->add();
    }

    /**
     * 获取砍价详情页信息
     * @param $orderNo
     * @return array|bool|string
     */
    public function getBargainDetailsInfo ($orderNo)
    {
        $data = $this->getBaseBargainInfo($orderNo);
        if ($data) {
            $BargainDetails = new GoodsBargainDetailsModel();
            $data['list'] = $BargainDetails->getBargainDetailsList($data['pid']);
            $data['bargain_count'] = $BargainDetails->getBargainCount($data['pid']);
            $data['is_bargain'] = $BargainDetails->checkUserIsBargain($data['pid']);
        }
        return $data;
    }

    /**
     * 获取砍价订单基本信息
     * @param $orderNo
     * @return array|bool|string
     */
    private function getBaseBargainInfo($orderNo)
    {
        $data = $this->alias('a')
            ->field('a.id pid,a.state bargain_state,a.limit_cut,a.state,b.nickname,b.header,c.total_fee,e.name,e.intro2,e.img')
            ->join('left join t_users b on a.uid = b.id')
            ->join('left join t_goods_order c on a.order_no = c.order_no')
            ->join('left join t_goods_order_child d on a.order_no = d.order_no')
            ->join('left join t_goods e on d.gid = e.id')
            ->where(array('a.order_no'=>$orderNo))
            ->find();
        if (is_array($data) && count($data) > 0) {
            return getUploadImgDir($data,array('user_head'=>'header','goods'=>'img'),1);
        }
        return false;
    }

    /**
     * 检查用户是否发起过砍价活动
     * @return array|false
     */
    public function checkBargainRight()
    {
        //当用户有正在砍价的或者已完成的砍价订单，则无法进行砍价
        $maps['uid'] = $this->uid;
        $maps['state'] = array('IN',array('1','3'));
        return $this->where($maps)->find();
    }

    /**
     * 获取订单砍价信息
     * @param $orderNo
     * @return array|bool
     */
    public function getOrderBargainInfo($orderNo)
    {
        $maps['order_no'] = $orderNo;
        $maps['state'] = '1';
        $data = $this->where($maps)->find();
        if (is_array($data) && count($data) > 0) {
            return $data;
        }
        return false;
    }
    //

    /**
     * 更新砍价状态
     * @param $id
     * @param $state
     * @return bool
     */
    public function updateBargainState($id, $state)
    {
        $maps['id'] = $id;
        $maps['uid'] = $this->uid;

        return $this->where($maps)->data(array('state'=>$state))->save();
    }

    public function getCurrentBargain()
    {
        $maps['uid'] = $this->uid;
        $maps['state'] = 1;

        return $this->where($maps)->find();
    }

}